<template>
	<view class="home">
		<u-sticky bgColor="#fff">
			<u-tabs :activeStyle="activeStyle" :list="navList" @click="navClick"></u-tabs>
		</u-sticky>
		<u-skeleton rows="4" title :loading='loading'>
			<view v-for="item in articleList">
				<article-item :articleListItem="item"></article-item>
			</view>
		</u-skeleton>
		<view class="editBtn" @click="toEdit">
			<u-icon name="edit-pen" size='30' color="#FFFFFF"></u-icon>
		</view>
	</view>
</template>

<script>
	const db = uniCloud.database()
	export default {
		data() {
			return {
				loading: true,
				activeStyle: {
					color: '#333',
					fontWeight: 'bold',
					transform: 'scale(1.08)'
				},
				inactiveStyle: {
					color: '#888',
					transform: 'scale(1)'
				},
				navList: [{
					name: '最新',
					type: 'publish_date'
				}, {
					name: '热门',
					type: 'view_count'
				}],
				articleList: []

			}
		},
		onLoad() {
			this.getArticleList()

		},
		methods: {
			navClick(item) {
				this.getArticleList(item.type)
				this.loading = true
			},
			toEdit() {
				uni.navigateTo({
					url: '/pages/editArticle/editArticle'
				})
			},
			getArticleList(type = 'publish_date') {
				const user = db.collection('uni-id-users').field('_id,nickname,avatar_file').getTemp()
				const article = db.collection('lxArticle').field(
					"title,user_id,description,picurls,comment_count,like_count,view_count,publish_date").getTemp()
				db.collection(article, user).orderBy(
					type, "desc").get().then(res => {
					this.articleList = res.result.data
					this.loading = false
					console.log(res)
				})
			}

		}
	}
</script>

<style lang="scss" scoped>
	.home {
		.editBtn {
			display: flex;
			justify-content: center;
			width: 120rpx;
			height: 120rpx;
			border-radius: 50%;
			box-shadow: 0 0 10px rgba(1, 153, 254, .8);
			background-color: #0199FE;
			position: fixed;
			right: 10rpx;
			bottom: 120rpx;

		}

	}
</style>